<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class UaddController extends Controller
{
    //设置首页
    public function getAdd()
    {
    	return view('users.add');
    }
    public function postInsert(Request $request)
    {
    	// dd($request->all());
    	
    	//判断用户名是否存在
    	// if(!$request->input('username')){
    	// 	return back()->with('error','用户名不能为空');
    	// }
    	$this->validate($request, [
        'username' => 'required|regex:/\w{6,18}/',
        'password' => 'required|regex:/\w{6,18}/',
        'repassword' => 'required|same:password',
        'email' => 'required|email',
        'phone' => 'required|regex:/1[3-8]\d{9}/',
    	],[
    	'username.required' => '用户名不能为空',
    	'username.regex' => '用户名格式不正确',
    	'password.required' => '密码不能为空',
    	'password.regex' => '密码格式不正确',
    	'repassword.required' => '重复密码不能为空',
    	'repassword.same' => '重复密码格式不正确',
    	'email.required' => '邮箱不能为空',
    	'email.email' => '邮箱格式不正确',
    	'phone.required' => '手机号不能为空',
    	'phone.regex' => '手机格式不正确',
    	]);

    	//获取其他数据
    	$data = $request->only(['username','email','password','phone',]);
    	// dd($data);
    	//随机token
    	$data['token'] = str_random(50);
    	//状态为1未激活
    	$data['status'] = 1;
    	//密码加密
    	$data['password'] = Hash::make($data['password']);
    	//处理文件上传
    	$data['pic'] = self::upload($request);
    	// dd($data);
    	//数据插入
    	$res = DB::table('user')->insert($data);
    	if($res){
    		return redirect('/admin/uadd/index')->with('success','添加成功');
    	}else{
    		return back()->with('error','数据添加失败');
    	}
    }

    public function getIndex(Request $request)
    {	
    	$num = $request->input('num',10);
    	// dd($res);
  		
    	// $users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
    	// dd($users);
    	if($request->input('keywords')){
    		$users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
    	}else{
    		$users = DB::table('user')->paginate($num);
    	}

    	return view('users.index',['users'=>$users,'request'=>$request->all()]);
    }

    //ajax删除用户
    public function postAjaxdelete(Request $request){
		$id = $request->input('id');
		$res = DB::table('user')->where('id',$id)->delete();
		//判断
		if($res){
			echo 1;
		}else{
			echo 0;
		}		

    }

    //修改用户
    public function getEdit(Request $request){
    	$id = $request->input('id');
    	$user = DB::table('user')->where('id',$id)->first();
    	// dd($user);
    	//解析模板
    	return view('users.edit',['user'=>$user]);
    }

    //数据修改
    public function postUpdate(Request $request){
    	// dd($request->all());
    	$id = $request->input('id');
    	// dd($id);
    	$data = $request->except(['_token','id']);
        if($request->hasFile('pic')){
    	    $data['pic'] = self::upload($request);
        }
    	// dd($data);
    	$res = DB::table('user')->where('id',$id)->update($data);
        // dd($res);
    	//判断
    	if($res){
    		return redirect('/admin/uadd/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','数据修改失败');
    	}

    }

    //图片上传
    static function upload(Request $request)
    {
    	//文件上传的form表单中 必须有 enctype="multipart/form-data" 
    	//检测是否有上传文件
    	if($request->hasFile('pic')){
    		//文件名称
    		$name = md5(time()+rand(111111,8999999));
    		//文件后缀名获取
    		$suffix = $request->file('pic')->getClientOriginalExtension();
    		$arr = ['jpg','png'];
    		//判断文件上传类型
    		if(!in_array($suffix,$arr)){
    			echo '上传文件不符合要求';die;
    		}
    		//将指定文件移动到指定位置
    		$request->file('pic')->move('./uploads/', $name.'.'.$suffix);
    		return '/uploads/'.$name.'.'.$suffix;
    	}
    }
}
